﻿Imports System.Net
Imports System.IO
Module DownloadClass
    Public Function GetFile(ByVal Link As String, Optional ByVal Path As String = Nothing)
        Dim LinkedFile = String.Empty
        Dim SaveFilename As String = String.Empty
        Try
            Dim fileReader As New WebClient()
            LinkedFile = Link
            If Link = "" Then
                Link = System.Environment.CurrentDirectory
            End If
            SaveFilename = LinkedFile.Substring(LinkedFile.LastIndexOf("/") + 1)
            If Not (System.IO.File.Exists(Path)) Then
                Directory.CreateDirectory(Path)
            End If
            fileReader.DownloadFile(LinkedFile, Path & "\" & SaveFilename)

        Catch ex As HttpListenerException
            MsgBox("Error accessing " + LinkedFile + " - " + ex.Message)
        Catch ex As Exception
            MsgBox("Error accessing " + LinkedFile + " - " + ex.Message)
        End Try
        Return 0
    End Function
End Module
